package org.yi.fc.entity;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.fc.model.Pager;
import org.yi.fc.utils.ParamUtils;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

public class FFriendLink extends Model<FFriendLink>{
	
	private static final Logger logger = LoggerFactory.getLogger(FFriendLink.class);

	private static final long serialVersionUID = 1L;
	
	public static final FFriendLink dao = new FFriendLink();
	
	/**
	 * 保存
	 * @param request
	 */
	public void save(HttpServletRequest request) {
		logger.trace("save friendlink, keywords:{}, url:{}", ParamUtils.get(request, "keyword"), ParamUtils.get(request, "url"));
		set("keyword", ParamUtils.get(request, "keyword"));
		set("url", ParamUtils.get(request, "url"));
		set("logo", ParamUtils.get(request, "logo"));
		set("status", ParamUtils.get(request, "status"));
		save();
	}
	
	/**
	 * 更新
	 * @param request
	 */
	public void update(HttpServletRequest request) {
		logger.trace("edit friendlink, id:{}", ParamUtils.get(request, "id"));
		set("keyword", ParamUtils.get(request, "keyword"));
		set("url", ParamUtils.get(request, "url"));
		set("logo", ParamUtils.get(request, "logo"));
		set("status", ParamUtils.get(request, "status"));
		set("id", ParamUtils.get(request, "id"));
		update();
	}
	
	/**
	 * 分页-查询所有
	 * @param pager
	 * @return
	 */
	public Page<FFriendLink> getPager(Pager pager) {
		return FFriendLink.dao.paginate(pager.getPn(), pager.getSize(), "select *", "from f_friend_link");
	}

	/**
	 * 删除友链, 直接删除数据库中记录
	 * @param ids
	 */
	public void delete(String... ids) {
		for(String id : ids) {
			deleteById(Long.parseLong(id));
		}
	}

	/**
	 * 禁用友链
	 * @param ids
	 */
	public void ban(String... ids) {
		String sql = "";
		for(String id : ids) {
			sql = "update f_friend_link set status = (status+1)%2 where id = ?";
			Db.update(sql, Long.parseLong(id));
		}
	}

	/**
	 * 禁用
	 * @param action
	 * @param ids
	 */
	public void ban(Integer action, String... ids) {
		String sql = "";
		for(String id : ids) {
			if(action == 0) {
				sql = "update f_friend_link set status = 0 where id = ?";
			} else if(action == 1) {
				sql = "update f_friend_link set status = 1 where id = ?";
			}
			Db.update(sql, Long.parseLong(id));
		}
	}

}
